package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


/**
 * 分类管理
 */

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类接口")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;


    /**
     * 分类分页查询
     *
     * @return
     */
    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> searchCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分类分页查询{}", categoryPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(classifyService.CountCategory(categoryPageQueryDTO));
        pageResult.setRecords(classifyService.SearchCategory(categoryPageQueryDTO));
        return Result.success(pageResult);
    }


    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @ApiOperation("删除分类")
    @DeleteMapping
    public Result shanchu(Integer id) {
        log.info("删除分类{}", id);
        int temp = classifyService.shanchu(id);
        if (temp == 0) {
            return Result.error("该分类包含菜品,无法删除");
        }
        if (temp == 2) {
            return Result.error("该分类包含套餐,无法删除");
        }
        return Result.success();
    }


    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result xiugai(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类{}", categoryDTO);
        classifyService.xiugai(categoryDTO);
        return Result.success();
    }


    /**
     * 启用禁用
     *
     * @param status
     * @return
     */
    @ApiOperation("启用禁用")
    @PostMapping("/status/{status}")
    public Result qi(@PathVariable short status, Long id) {
        log.info("启用禁用{}{}", status, id);
        classifyService.qi(status, id);
        return Result.success();
    }


    /**
     * 新增
     *
     * @param categoryDTO
     * @return
     */
    @ApiOperation("新增")
    @PostMapping
    public Result add(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增{}", categoryDTO);
        classifyService.add(categoryDTO);
        return Result.success();
    }

    @ApiOperation("根据类型查询")
    @GetMapping("/list")
    public Result<ArrayList<Category>> TypeSearch(Integer type) {
        log.info("根据类型查询");
        ArrayList<Category> category = new ArrayList<>();
        category = classifyService.typeSearch(type);
        return Result.success(category);
    }
}
